package club.terroir.server;
 
//   club.terroir.server.FilterIdentification
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import club.terroir.server.user.UtilisateurFactory;
import club.terroir.server.user.Utilisateur_SS;
import club.terroir.server.util.MailSender;

public class FilterIdentification implements Filter {

	//private String landingDefaultPage = "Club_terroir.jsp";
	public static boolean DEBUG = true;
	@Override
	public void init(FilterConfig arg0) throws ServletException {   
	}

	@Override
	public void destroy() {		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		request.setAttribute("commentLogin", "Login request");
		Object user = request.getSession().getAttribute("user");
		String action = "" + request.getParameter("action");
		if (action.equals("login")) {
			user = getUserFromEmail(request);
			request.getSession().setAttribute("user", user);		
		}else if (action.equals("displayRegisterPage")) {
			RequestDispatcher dispatcher = req.getRequestDispatcher("/register.jsp");
			dispatcher.forward(req, resp);
			return;
		} else if (action.equals("register")) {
			user = getUserFromRegister(request);
			if (user == null) {
				RequestDispatcher dispatcher = req.getRequestDispatcher("/register.jsp");
				dispatcher.forward(req, resp);
				return;
			} else {
				request.getSession().setAttribute("user", user);
				
			}
		} else if (action.equals("sendMeMyPassword")){
			String toEmail = request.getParameter("email");
			System.out.println("sendMeMyPassword22222 email:"+toEmail);
			request.setAttribute("commentLogin", "Un email a �t� envoy� � "+toEmail);
			MailSender.getInstance().sendMessage(toEmail, "bertrand.guiral@gmail.com", "Ton mot de passe", "zorg31secret");
			dispalyLoginPage(request,response);
			return;
		} 
		if (DEBUG && user==null){
			user = new Utilisateur_SS("bertrand.guiral@gmail.com","Bertrand","GUIRAL","xxx","","","31200","Toulouse","000000");
			request.getSession().setAttribute("user", user);
		}
		if (user == null) {
			System.out.println("----------------------- action " + action);
			
			RequestDispatcher dispatcher = req.getRequestDispatcher("/login.jsp");
			dispatcher.forward(req, resp);
			return;
		} else {
			
			chain.doFilter(req, resp);
		}

	}
	
	private void dispalyLoginPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/login.jsp");
		dispatcher.forward(request, response);
	}

	

	private Utilisateur_SS getUserFromEmail(HttpServletRequest request) {
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		Utilisateur_SS user = null;
		if (email == null) {
			user = null;
		} else if (email.length() == 0) {
			user = null;
		} else {
			user = UtilisateurFactory.getInstance().getUserByEmail(email);
		}
		return user;
	}

	private Utilisateur_SS getUserFromRegister(HttpServletRequest request) {
		String idUser = request.getParameter("idUser");
		String email = request.getParameter("email");
		String name = request.getParameter("name");
		String firstName = request.getParameter("firstName");
		String address_1 = request.getParameter("address_1");
		String address_2 = request.getParameter("address_2");
		String codePostal = request.getParameter("codePostal");
		String ville = request.getParameter("ville");
		String phoneNumber = request.getParameter("phoneNumber");
		String password = request.getParameter("password");
		String password2 = request.getParameter("password2");
 
		Utilisateur_SS utilisateur = null;
		utilisateur = UtilisateurFactory.getInstance().getUserByEmail(email);
		if (utilisateur == null) {
			try {

				utilisateur = new Utilisateur_SS(email, name, firstName, password,
						address_1, address_2, codePostal, ville, phoneNumber);
				UtilisateurFactory.getInstance().persist(utilisateur);

				return utilisateur;
			} catch (Exception e) {
				request.setAttribute("error", e.getMessage());
				return null;
			}
		} else {
			request.setAttribute("error", email + " est d�j� enregistr� !!");
			return null;
		}
	}

	private boolean isParamOk(String s) {
		boolean b = true;
		if (s == null) {
			b = false;
		} else if (s.trim().length() == 0) {
			b = false;
		}
		return b;
	}

}
